﻿/*
-----------------------------------------------------------------------------
This source file is part of Cell Cloud.

Copyright (c) 2009-2012 Cell Cloud Team - cellcloudproject@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-----------------------------------------------------------------------------
*/

#ifndef CellSingleFileStorage_h
#define CellSingleFileStorage_h

#include "CellPrerequisites.h"
#include "CellStorage.h"
#include "CellStorageFactory.h"
#include "CellSingleFileResultSet.h"
#include "CellCharBuffer.h"

namespace cell
{

#define SFS_BUFFER_SIZE 8192

/// 存储缓存
class SFSBuffer;

/** 单一文件存储器。
@note
	文件存储器设计为快速地文件数据写入与读取，但不要将文件存储器作为“文件缓存”使用。
	存储器的设计目标是对文件进行快速读取或写入，尽可能地减少I/O次数，
	虽然其具备“缓存”特征，但是并不是为实现文件缓存而设计。
@author
	Jiangwei Xu
*/
class _CellExport SingleFileStorage : public Storage
{
public:
	/** 构造函数。
	*/
	SingleFileStorage(const String& name);

	/** 析构函数。
	*/
	virtual ~SingleFileStorage(void);

public:
	/// 文件是否存在
	static const String FIELD_EXIST;

	/// 文件数据
	static const String FIELD_DATA;

	/// 文件长度
	static const String FIELD_SIZE;

public:
	/**
	*/
	const String& getTypeName(void) const;

	/** 打开存储器。
	@note
		该方法不是打开文件，而是开启存储器。
	*/
	bool open(const String& filename);

	/** 关闭存储器。
	@note
		该方法不是关闭文件，而是关闭存储器。
	*/
	void close(void);

	/**
	*/
	ResultSet* createResultSet(void);

	/**
	*/
	void destroyResultSet(ResultSet* resultSet);

	/**
	*/
	void store(ResultSet* resultSet, const String& statement);

	/**
	*/
	void store(ResultSet* resultSet, const Schema* schema);

public:
	/** 设置文件存储缓存片段区大小。
	@note size 值必须是2的 N 次幂。
	*/
	void setFetchSize(uint size);

	/** 返回文件存储缓存片段区大小。
	*/
	uint getFetchSize(void) const;

	/** 设置文件片段最大缓存数量。
	*/
	void setMaxFetchNum(int num);

	/** 返回文件片段最大缓存数量。
	*/
	int getMaxFetchNum(void) const;

	/** 返回读文件操作声明。
	*/
	String getReadStatement(void);

	/** 返回写文件操作声明。
	*/
	String getWriteStatement(void);

private:
	/**
	*/
	void changeSize(int64 size);

	/**
	*/
	bool read(void);

	/**
	*/
	void write(char* src, int64 offset, int length);

	/**
	*/
	SFSBuffer* findBuffer(int64 offset);

	/**
	*/
	void flushBuffer(SFSBuffer* buffer);

	/**
	*/
	void mergeAndFlushBuffer(const std::vector<SFSBuffer*>& list);

	/**
	*/
	void checkFlush(void);

private:
	typedef HashMap<int64, SFSBuffer*> SFSBuffers;

	uint mFetchSize;
	int mMaxFetchNum;

	String mFileName;
	int64 mFileSize;

	CharBuffer* mReadBuffer;
	SFSBuffers mSFSBuffers;
	int64 mTotalLength;

	CELL_MUTEX(mMonitor);

	friend class SingleFileResultSet;
};



/**
*/
class _CellPrivate SFSBuffer : public CharBuffer
{
public:
	/**
	*/
	SFSBuffer(int64 position, int capacity);

	/**
	*/
	virtual ~SFSBuffer(void);

public:
	/**
	*/
	int64 filePosition(void) const;

public:
	/**
	*/
	static bool compare(const SFSBuffer* buf1, const SFSBuffer* buf2);

private:
	int64 mPosition;
};



/** 单一文件储器工厂。
@author
	Jiangwei Xu
*/
class _CellExport SingleFileStorageFactory : public StorageFactory
{
public:
	/**
	*/
	SingleFileStorageFactory(void);

	/**
	*/
	virtual ~SingleFileStorageFactory(void);

public:
	/// 
	static const String FACTORY_TYPE_NAME;

public:
	/**
	*/
	Storage* createInstance(const String& instanceName);

	/**
	*/
	void destroyInstance(Storage* instance);

protected:
	/// 初始化 Meta 数据
	void initMetaData(void) const;
};
} // end namespace cell

#endif // CellSingleFileStorage_h
